//车辆附件管理

interface ICarAttachmentEditViewModel extends ng.IScope {
    /**
     * 驾驶证上传组件
     */
    driverUploader: any;
    /**
     * 挂车证上传组件
     */
    trailerUploader: any;
    /**
     * 行驶证上传组件
     */
    travelUploader: any;
    /**
     * 身份证上传组件
     */
    idCardUploader: any;
    /**
     * 车辆照片
     */
    carPicUploader: any;
     /**
     * 道路运输证
     */
    roadTransportCardUploader:any
    /**
     * 道路运输经营许可证
     */
    roadTransportLicenseUploader:any;
    /**
     * 从业资格证
     */
    occupationalLicenseUploader:any;
    /**
     * 真实头像
     */
    realHeadImgUploder:any
    uploader:any;
    /**
     * 保存附件
     */
    save: () => void;
    /**
     * 点击图片时候触发，用以旋转放大图片
     */
    handleImgClick:(item:any)=>void;
    /**
     * 用以旋转放大的图片的路径
     */
    selectedImgSrc:string;
    

    demoUploader:any;

    attachList:OssUploaderItem[];

}

class CarAttachmentEditController {
    //uploaderList: AddCarAttachmentDetail[];
    baseImageUrl: string;
    carId: string;
    uploaderList=[];
    
    uploaderNameList=['driverUploader','trailerUploader','travelUploader','idCardUploader','carPicUploader','roadTransportCardUploader','roadTransportLicenseUploader','occupationalLicenseUploader','realHeadImgUploder']
    constructor(private $scope: ICarAttachmentEditViewModel, private routeService: routeService, private carService: ICarService,
        private $location: ng.ILocationService, private $http: ng.IHttpService, private $ngBootbox: BootboxService,private commonService:ICommonService,
        private ossUploaderService:ossUploaderService
        ) {
        this.baseImageUrl = this.routeService.getBaseUrl() + "attachment?id=";
        //this.uploaderList = [];
        this.init();
    }

    init(): void {
        this.carId = this.$location.search().carId;
        this.carId = JSON.parse(window.localStorage.getItem("car").toString()).carId;
        this.$scope.uploader = null;
        
     
        this.$scope.handleImgClick=(item)=>{
            this.$scope.selectedImgSrc=item.imgSrc;
        }
        this.$scope.save=this.save;

        const OssFile = this.ossUploaderService.getOssFile();
        this.$scope.attachList=[
            {
                title:'驾驶证',
                uploader:null,
                uploaderName:'driverUploader',

            },
            {
                title:'挂车证',
                uploader:null,
                uploaderName:'trailerUploader',

            },
            {
                title:'行驶证',
                uploader:null,
                uploaderName:'travelUploader',

            },
            {
                title:'身份证',
                uploader:null,
                uploaderName:'idCardUpload',

            },
            {
                title:'车辆照片',
                uploader:null,
                uploaderName:'carPicUploader',

            },
            {
                title:'道路运输证',
                uploader:null,
                uploaderName:'roadTransportCardUploader',

            },
            {
                title:'道路运输经营许可证',
                uploader:null,
                uploaderName:'roadTransportLicenseUploader',

            },
            {
                title:'从业资格证',
                uploader:null,
                uploaderName:'occupationalLicenseUploader',

            },
            {
                title:'真实头像',
                uploader:null,
                uploaderName:'realHeadImgUploder',

            }
        ]
        this.$scope.attachList.forEach((item,index)=>{
            item.uploader=new OssFile(this.$scope,item.uploader,{uploaderList:this.uploaderList,tag:index+1});
            item.uploader.onSuccess=(it)=>{
                console.log('onsuccess',it);
                this.onSuccessItem(it.fileName,it.imgSrc,index+1,it._file)
    
            }

        })

        this.loadImg()

    }
    loadImg(){
        this.carService.getDetail(this.carId).then((res)=>{
          
            this.$scope.attachList[0].uploader.addByPath(res.data.drivingLicenseImageKey);
            this.$scope.attachList[1].uploader.addByPath(res.data.trailerLicenseKey);
            this.$scope.attachList[2].uploader.addByPath(res.data.vehicleLicenseKey);
            this.$scope.attachList[3].uploader.addByPath(res.data.identityCardImageKey);
            this.$scope.attachList[4].uploader.addByPath(res.data.vehiclePhotoKey);
            this.$scope.attachList[5].uploader.addByPath(res.data.roadTransportKey);
            this.$scope.attachList[6].uploader.addByPath(res.data.businessLicenseKey);
            this.$scope.attachList[7].uploader.addByPath(res.data.qualificationKey);
            this.$scope.attachList[8].uploader.addByPath(res.data.certificateImageKey);
        });
    }

    /**
     * 选择上传文件后
     */
    onSuccessItem = (fileName: string, filePath: string, tag: number, file?: any) => {
        file.fileId = filePath;
        let attachment = {
            attachmentId: "",
            attachmentName: fileName,
            attachmentPath: filePath,
            attachmentTag: tag.toString()
        };
        this.uploaderList.push(attachment);
    }
    /**
     * 删除附件
     */
    onDelete = (item: any, num: number) => {
        
        var types = item.types || num;
        this.commonService.onDelete(item, this.uploaderList);
        
        this.$scope[this.uploaderNameList[types-1]].isLoad = true;
        
    }
   
    /**
     * 下载附件
     */
    onDownload = (item: any) => {
        window.location.href = this.baseImageUrl + item.imgSrc;
    }

    save=()=>{
        console.log(this.uploaderList);
        if(this.uploaderList.length===0){
            this.$ngBootbox.alert('没有需要保存的图片');
            return;
        }
        this.carService.AddCCPCarAttachment(this.carId,this.uploaderList).then(res=>{
            // console.log(res);
           
            this.$scope.attachList.forEach(item=>{
                item.uploader.clearQueue();
            })
            this.uploaderList=[];
            this.loadImg()
        })
    }

}

angular.module("tms").controller("carAttachmentEditController", ["$scope", "routeService", "carService", "$location", "$http", "$ngBootbox","commonService","ossUploaderService", CarAttachmentEditController]);
